package com.wwwzh.himalayafm.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.databinding.ActivityMainBinding;
import com.wwwzh.himalayafm.entity.AdEntity;
import com.wwwzh.himalayafm.entity.User;
import com.wwwzh.himalayafm.retrofit.HttpService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new SleepThread().start();

    }
    private class SleepThread extends Thread implements Callback<AdEntity> {
        @Override
        public void run() {
            super.run();
            try {
                Thread.sleep(2*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            HttpService.AdGetService service = new Retrofit.Builder()
                    .baseUrl("http://adse.ximalaya.com/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
                    .create(HttpService.AdGetService.class);
            service.getAdCover().enqueue(this);
        }

        @Override
        public void onResponse(Call<AdEntity> call, Response<AdEntity> response) {
            ActivityMainBinding binding= DataBindingUtil.setContentView(MainActivity.this,R.layout.activity_main);
            User user=new User();
            user.setIcon(response.body().getData().get(0).getCover());
            binding.setUser(user);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3*1000);
                        Intent intent = new Intent();
                        intent.setClass(MainActivity.this, XmlyActivity.class);
                        startActivity(intent);
                        finish();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        @Override
        public void onFailure(Call<AdEntity> call, Throwable t) {

        }
    }

    private class SleepThread2 extends Thread {

        @Override
        public void run() {
            super.run();
            try {
                Thread.sleep(3 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, XmlyActivity.class);
            startActivity(intent);
            finish();
        }
    }

}
